const express = require("express")
const router = express.Router()
const banner = require("./data/banner.json")
const url = require("url")
const SQLConnect = require("./SQLConnect")
const newGoods = require("./data/newsGoods.json")
const personStore = require("./data/personStore.json")
const suitData = require("./data/suit.json")
const jwt = require("jsonwebtoken")
const config = require("./util/config")
const md5 = require("js-md5")

/**
 * @swagger
 * /api/banner:
 *   get:
 *     summary: 获取轮播图数据
 *     description: 获取首页轮播图信息
 *     tags: [首页模块]
 *     responses:
 *       200:
 *         description: 成功获取轮播图数据
 *         content:
 *           application/json:
 *             schema:
 *               type: array
 *               items:
 *                 type: object
 *                 properties:
 *                   id:
 *                     type: integer
 *                     description: banner ID
 *                     example: 1
 *                   image:
 *                     type: string
 *                     description: banner 图片 URL
 *                     example: "/images/banner1.jpg"
 *                   title:
 *                     type: string
 *                     description: banner 标题
 *                     example: "精选商品推荐"
 */
router.get("/banner", (req, res) => {
    res.send(banner)
})

/**
 * @swagger
 * /api/beauty:
 *   get:
 *     summary: 获取美业快讯
 *     description: 分页获取美业快讯信息
 *     tags: [资讯模块]
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *     responses:
 *       200:
 *         description: 成功获取美业快讯
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: integer
 *                   description: 状态码
 *                   example: 200
 *                 data:
 *                   type: array
 *                   description: 响应数据
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: integer
 *                         description: 快讯 ID
 *                         example: 1
 *                       title:
 *                         type: string
 *                         description: 快讯标题
 *                         example: "美业新趋势"
 *                       content:
 *                         type: string
 *                         description: 快讯内容
 *                         example: "最新美业发展动态"
 *                       image:
 *                         type: string
 *                         description: 快讯图片
 *                         example: "/images/news1.jpg"
 *                       time:
 *                         type: string
 *                         description: 发布时间
 *                         example: "2024-01-01 10:00:00"
 *                 msg:
 *                   type: string
 *                   description: 响应消息
 *                   example: "获取成功"
 *       500:
 *         description: 暂无数据
 */
router.get("/beauty", (req, res) => {
    // 接受前端传递的参数
    var page = url.parse(req.url, true).query.page || 1;
    // 编写数据库语句
    const sql = "select * from beauty limit 10 offset " + (page - 1) * 10;
    SQLConnect(sql, null, result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result
            })
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            })
        }
    })
})


/**
 * @swagger
 * /api/newgoods:
 *   get:
 *     summary: 获取最新商品
 *     description: 获取最新商品列表
 *     tags: [商品模块]
 *     responses:
 *       200:
 *         description: 成功获取最新商品
 *         content:
 *           application/json:
 *             schema:
 *               type: array
 *               items:
 *                 type: object
 *                 properties:
 *                   id:
 *                     type: integer
 *                     description: 商品 ID
 *                     example: 1
 *                   name:
 *                     type: string
 *                     description: 商品名称
 *                     example: "美容护肤套装"
 *                   price:
 *                     type: string
 *                     description: 商品价格
 *                     example: "299.00"
 *                   image:
 *                     type: string
 *                     description: 商品图片
 *                     example: "/images/goods1.webp"
 *                   description:
 *                     type: string
 *                     description: 商品描述
 *                     example: "高品质美容护肤产品"
 */
router.get("/newgoods", (req, res) => {
    res.send(newGoods)
})

/**
 * @swagger
 * /api/personstore:
 *   get:
 *     summary: 获取人才库
 *     description: 获取人才库信息
 *     tags: [人才模块]
 *     responses:
 *       200:
 *         description: 成功获取人才库数据
 *         content:
 *           application/json:
 *             schema:
 *               type: array
 *               items:
 *                 type: object
 *                 properties:
 *                   id:
 *                     type: integer
 *                     description: 人才 ID
 *                     example: 1
 *                   name:
 *                     type: string
 *                     description: 人才姓名
 *                     example: "张美丽"
 *                   position:
 *                     type: string
 *                     description: 职位
 *                     example: "高级美容师"
 *                   experience:
 *                     type: string
 *                     description: 工作经验
 *                     example: "5年经验"
 *                   skill:
 *                     type: string
 *                     description: 技能
 *                     example: "面部护理,身体护理"
 *                   image:
 *                     type: string
 *                     description: 人才头像
 *                     example: "/images/person1.jpg"
 */
router.get("/personstore", (req, res) => {
    res.send(personStore)
})

/**
 * @swagger
 * /api/recommendgoods:
 *   get:
 *     summary: 获取推荐商品
 *     description: 获取为你推荐的商品列表
 *     tags: [商品模块]
 *     responses:
 *       200:
 *         description: 成功获取推荐商品
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: integer
 *                   description: 状态码
 *                   example: 200
 *                 data:
 *                   type: array
 *                   description: 响应数据
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: integer
 *                         description: 商品 ID
 *                         example: 1
 *                       name:
 *                         type: string
 *                         description: 商品名称
 *                         example: "推荐美容套装"
 *                       price:
 *                         type: string
 *                         description: 商品价格
 *                         example: "399.00"
 *                       image:
 *                         type: string
 *                         description: 商品图片
 *                         example: "/images/goods2.webp"
 *                       description:
 *                         type: string
 *                         description: 商品描述
 *                         example: "精选推荐商品"
 *                 msg:
 *                   type: string
 *                   description: 响应消息
 *                   example: "获取成功"
 *       500:
 *         description: 暂无数据
 */
router.get("/recommendgoods", (req, res) => {
    const sql = "select * from goods";
    SQLConnect(sql, null, result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result
            })
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            })
        }
    })
})

/**
 * @swagger
 * /api/goods/details:
 *   get:
 *     summary: 获取商品详情
 *     description: 根据商品ID获取商品详细信息
 *     tags: [商品模块]
 *     parameters:
 *       - in: query
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 商品ID
 *     responses:
 *       200:
 *         description: 成功获取商品详情
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: integer
 *                   description: 状态码
 *                   example: 200
 *                 data:
 *                   type: object
 *                   description: 商品详情数据
 *                   properties:
 *                     id:
 *                       type: integer
 *                       description: 商品 ID
 *                       example: 1
 *                     name:
 *                       type: string
 *                       description: 商品名称
 *                       example: "美容护肤套装"
 *                     price:
 *                       type: string
 *                       description: 商品价格
 *                       example: "299.00"
 *                     image:
 *                       type: string
 *                       description: 商品图片
 *                       example: "/images/goods1.webp"
 *                     description:
 *                       type: string
 *                       description: 商品描述
 *                       example: "高品质美容护肤产品详情"
 *                 msg:
 *                   type: string
 *                   description: 响应消息
 *                   example: "获取成功"
 *       500:
 *         description: 暂无数据
 */
router.get("/goods/details", (req, res) => {
    var id = url.parse(req.url, true).query.id;
    const sql = "select * from goods where id=?";
    SQLConnect(sql, [id], result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result[0]
            })
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            })
        }
    })
})

/**
 * @swagger
 * /api/comment/goods:
 *   get:
 *     summary: 获取商品评价
 *     description: 根据商品ID获取商品评价信息
 *     tags: [评价模块]
 *     parameters:
 *       - in: query
 *         name: id
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 商品ID
 *     responses:
 *       200:
 *         description: 成功获取商品评价
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: integer
 *                   description: 状态码
 *                   example: 200
 *                 data:
 *                   type: array
 *                   description: 评价数据
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: integer
 *                         description: 评论 ID
 *                         example: 1
 *                       name:
 *                         type: string
 *                         description: 评论者姓名
 *                         example: "用户123"
 *                       icon:
 *                         type: string
 *                         description: 评论者头像
 *                         example: "/images/avatar1.jpg"
 *                       rate:
 *                         type: integer
 *                         description: 评分
 *                         example: 5
 *                       text:
 *                         type: string
 *                         description: 评论内容
 *                         example: "商品质量很好，非常满意"
 *                       time:
 *                         type: string
 *                         description: 评论时间
 *                         example: "2024-01-01 10:00:00"
 *                       images:
 *                         type: array
 *                         items:
 *                           type: string
 *                         description: 评论图片
 *                         example: ["/images/comment1.jpg", "/images/comment2.jpg"]
 *       500:
 *         description: 暂无数据
 */
router.get("/comment/goods", (req, res) => {
    var id = url.parse(req.url, true).query.id || 1;
    const sql = "select * from comment where id=?";
    SQLConnect(sql, [id], result => {
        if (result.length > 0) {
            let images = result[0].images.split(",")
            res.send({
                status: 200,
                data: [{
                    images,
                    id: result[0].id,
                    icon: result[0].icon,
                    name: result[0].name,
                    rate: result[0].rate,
                    time: result[0].time,
                    text: result[0].text
                }]
            })
        } else {
            res.send({
                status: 500,
                data: "暂无数据"
            })
        }
    })
})

/**
 * @swagger
 * /api/comment/all:
 *   get:
 *     summary: 获取所有评价
 *     description: 获取所有商品的评价信息
 *     tags: [评价模块]
 *     responses:
 *       200:
 *         description: 成功获取所有评价
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: integer
 *                   description: 状态码
 *                   example: 200
 *                 data:
 *                   type: array
 *                   description: 所有评价数据
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: integer
 *                         description: 评论 ID
 *                         example: 1
 *                       name:
 *                         type: string
 *                         description: 评论者姓名
 *                         example: "用户123"
 *                       icon:
 *                         type: string
 *                         description: 评论者头像
 *                         example: "/images/avatar1.jpg"
 *                       rate:
 *                         type: integer
 *                         description: 评分
 *                         example: 5
 *                       text:
 *                         type: string
 *                         description: 评论内容
 *                         example: "商品质量很好，非常满意"
 *                       time:
 *                         type: string
 *                         description: 评论时间
 *                         example: "2024-01-01 10:00:00"
 *                       images:
 *                         type: array
 *                         items:
 *                           type: string
 *                         description: 评论图片
 *                         example: ["/images/comment1.jpg", "/images/comment2.jpg"]
 *       500:
 *         description: 暂无数据
 */
router.get("/comment/all", (req, res) => {
    const sql = "select * from comment"
    SQLConnect(sql, null, result => {
        if (result.length > 0) {
            let currentResult = []
            for (let i = 0; i < result.length; i++) {
                let images = result[i].images.split(",")
                let temp = {
                    images,
                    id: result[i].id,
                    icon: result[i].icon,
                    name: result[i].name,
                    rate: result[i].rate,
                    time: result[i].time,
                    text: result[i].text
                }
                currentResult.push(temp)
            }
            res.send({
                status: 200,
                data: currentResult
            })
        } else {
            res.send({
                status: 500,
                data: "暂无数据"
            })
        }
    })
})

/**
 * @swagger
 * /api/place:
 *   get:
 *     summary: 获取选址列表
 *     description: 分页获取选址列表数据
 *     tags: [选址模块]
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: location
 *         schema:
 *           type: string
 *         description: 位置筛选
 *     responses:
 *       200:
 *         description: 成功获取选址列表
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: integer
 *                   description: 状态码
 *                   example: 200
 *                 data:
 *                   type: array
 *                   description: 选址列表数据
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: integer
 *                         description: 选址 ID
 *                         example: 1
 *                       name:
 *                         type: string
 *                         description: 选址名称
 *                         example: "市中心商业区店铺"
 *                       location:
 *                         type: string
 *                         description: 选址位置
 *                         example: "北京市朝阳区"
 *                       price:
 *                         type: string
 *                         description: 选址价格
 *                         example: "15000/月"
 *                       area:
 *                         type: string
 *                         description: 选址面积
 *                         example: "120平米"
 *                       image:
 *                         type: string
 *                         description: 选址图片
 *                         example: "/images/place1.jpg"
 *       500:
 *         description: 暂无数据
 */
router.get("/place", (req, res) => {
    var page = url.parse(req.url, true).query.page || 1;
    var location = url.parse(req.url, true).query.location;
    const sql = "select * from place limit 10 offset " + (page - 1) * 10
    SQLConnect(sql, null, result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result
            })
        } else {
            res.send({
                status: 500,
                data: "暂无数据"
            })
        }
    })
})

/**
 * @swagger
 * /api/place/details:
 *   get:
 *     summary: 获取选址详情
 *     description: 根据选址ID获取选址详细信息
 *     tags: [选址模块]
 *     parameters:
 *       - in: query
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 选址ID
 *     responses:
 *       200:
 *         description: 成功获取选址详情
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: integer
 *                   description: 状态码
 *                   example: 200
 *                 data:
 *                   type: object
 *                   description: 选址详情数据
 *                   properties:
 *                     id:
 *                       type: integer
 *                       description: 选址 ID
 *                       example: 1
 *                     name:
 *                       type: string
 *                       description: 选址名称
 *                       example: "市中心商业区店铺"
 *                     location:
 *                       type: string
 *                       description: 选址位置
 *                       example: "北京市朝阳区"
 *                     price:
 *                       type: string
 *                       description: 选址价格
 *                       example: "15000/月"
 *                     area:
 *                       type: string
 *                       description: 选址面积
 *                       example: "120平米"
 *                     image:
 *                       type: string
 *                       description: 选址图片
 *                       example: "/images/place1.jpg"
 *       500:
 *         description: 暂无数据
 */
router.get("/place/details", (req, res) => {
    var id = url.parse(req.url, true).query.id;
    const sql = "select * from place where id=?"
    SQLConnect(sql, [id], result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result[0]
            })
        } else {
            res.send({
                status: 500,
                data: "暂无数据"
            })
        }
    })
})


/**
 * @swagger
 * /api/suit:
 *   get:
 *     summary: 获取套装列表
 *     description: 分页获取套装列表数据
 *     tags: [套装模块]
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: type
 *         schema:
 *           type: string
 *         description: 套装类型筛选
 *     responses:
 *       200:
 *         description: 成功获取套装列表
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: integer
 *                   description: 状态码
 *                   example: 200
 *                 data:
 *                   type: array
 *                   description: 套装列表数据
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: integer
 *                         description: 套装 ID
 *                         example: 1
 *                       name:
 *                         type: string
 *                         description: 套装名称
 *                         example: "经典商务套装"
 *                       type:
 *                         type: string
 *                         description: 套装类型
 *                         example: "商务"
 *                       price:
 *                         type: number
 *                         description: 套装价格
 *                         example: 299.99
 *                       image:
 *                         type: string
 *                         description: 套装图片
 *                         example: "/images/suit1.jpg"
 *                       description:
 *                         type: string
 *                         description: 套装描述
 *                         example: "经典商务套装，适合正式场合"
 *       500:
 *         description: 暂无数据
 */
router.get("/suit", (req, res) => {
    res.send(suitData)
})


/**
 * @swagger
 * /api/person/list:
 *   get:
 *     summary: 获取人员列表
 *     description: 分页获取人员列表数据
 *     tags: [人员模块]
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: position
 *         schema:
 *           type: string
 *         description: 职位筛选
 *     responses:
 *       200:
 *         description: 成功获取人员列表
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: integer
 *                   description: 状态码
 *                   example: 200
 *                 data:
 *                   type: array
 *                   description: 人员列表数据
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: integer
 *                         description: 人员 ID
 *                         example: 1
 *                       name:
 *                         type: string
 *                         description: 人员姓名
 *                         example: "张三"
 *                       position:
 *                         type: string
 *                         description: 职位
 *                         example: "高级工程师"
 *                       department:
 *                         type: string
 *                         description: 部门
 *                         example: "技术部"
 *                       avatar:
 *                         type: string
 *                         description: 头像
 *                         example: "/images/avatar1.jpg"
 *                       email:
 *                         type: string
 *                         description: 邮箱
 *                         example: "zhangsan@example.com"
 *       500:
 *         description: 暂无数据
 */
router.get("/person/list", (req, res) => {
    var page = url.parse(req.url, true).query.page || 1;
    const sql = "select * from person limit 4 offset " + (page - 1) * 4
    SQLConnect(sql, null, result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result
            })
        } else {
            res.send({
                status: 500,
                data: "暂无数据"
            })
        }
    })
})

/**
 * @swagger
 * /api/person/details:
 *   get:
 *     summary: 获取人员详情
 *     description: 根据人员ID获取人员详细信息
 *     tags: [人员模块]
 *     parameters:
 *       - in: query
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 人员ID
 *     responses:
 *       200:
 *         description: 成功获取人员详情
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: integer
 *                   description: 状态码
 *                   example: 200
 *                 data:
 *                   type: object
 *                   description: 人员详情数据
 *                   properties:
 *                     id:
 *                       type: integer
 *                       description: 人员 ID
 *                       example: 1
 *                     name:
 *                       type: string
 *                       description: 人员姓名
 *                       example: "张三"
 *                     position:
 *                       type: string
 *                       description: 职位
 *                       example: "高级工程师"
 *                     department:
 *                       type: string
 *                       description: 部门
 *                       example: "技术部"
 *                     avatar:
 *                       type: string
 *                       description: 头像
 *                       example: "/images/avatar1.jpg"
 *                     email:
 *                       type: string
 *                       description: 邮箱
 *                       example: "zhangsan@example.com"
 *       500:
 *         description: 暂无数据
 */
router.get("/person/details", (req, res) => {
    var id = url.parse(req.url, true).query.id;
    const sql = "select * from person where id=?"
    SQLConnect(sql, [id], result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result[0]
            })
        } else {
            res.send({
                status: 500,
                data: "暂无数据"
            })
        }
    })
})


/**
 * @swagger
 * /api/login:
 *   post:
 *     summary: 用户登录
 *     description: 用户登录接口，返回JWT token
 *     tags: [用户认证]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - tel
 *               - password
 *               - verify
 *             properties:
 *               tel:
 *                 type: string
 *                 description: 手机号
 *                 example: "13800138000"
 *               password:
 *                 type: string
 *                 description: 密码
 *                 example: "123456"
 *               verify:
 *                 type: string
 *                 description: 验证码
 *                 example: "iwenwiki"
 *     responses:
 *       200:
 *         description: 登录成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: integer
 *                   example: 200
 *                 token:
 *                   type: string
 *                   description: JWT token
 *                 username:
 *                   type: string
 *                   description: 用户名
 *       401:
 *         description: 用户名密码错误
 *       405:
 *         description: 验证失败
 */
router.post("/login", (req, res) => {
    const { tel, password, verify } = req.body;
    if (verify == md5("iwenwiki")) {
        const sql = "select * from user where `phone`=? AND `password`=?";
        SQLConnect(sql, [tel, password], result => {
            if (result.length > 0) {
                // 生成token
                const token = jwt.sign({
                    id: result[0].id,
                    username: result[0].phone
                }, config.jwtSecret)
                res.send({
                    status: 200,
                    token,
                    username: result[0].phone
                })
            } else {
                res.send({
                    status: 401,
                    data: "用户名密码错误"
                })
            }
        })
    } else {
        res.send({
            status: 405,
            msg: "验证失败"
        })
    }

})

/**
 * @swagger
 * /api/register:
 *   post:
 *     summary: 用户注册
 *     description: 用户注册接口
 *     tags: [用户认证]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - password
 *               - phone
 *             properties:
 *               password:
 *                 type: string
 *                 description: 密码
 *                 example: "123456"
 *               phone:
 *                 type: string
 *                 description: 手机号
 *                 example: "13800138000"
 *     responses:
 *       200:
 *         description: 注册成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 注册成功
 *       401:
 *         description: 注册失败
 */
router.post("/register", (req, res) => {
    const { password, phone } = req.body;
    // 随机生成用户名
    let chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
        'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
    function getRandom(num) {
        var res = ""
        for (var i = 0; i < num; i++) {
            var usernameSQ = Math.ceil(Math.random() * 35)
            res += chars[usernameSQ]
        }
        return res;
    }
    const sql = "insert into user values (null,?,?,?)"
    SQLConnect(sql, [getRandom(6), password, phone], result => {
        if (result.affectedRows) {
            res.send({
                status: 200,
                message: "注册成功"
            })
        } else {
            res.send({
                status: 401,
                message: "注册失败"
            })
        }
    })
})

/**
 * @swagger
 * /api/order/all:
 *   get:
 *     summary: 获取全部订单
 *     description: 获取用户的所有订单
 *     tags: [订单模块]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 成功获取订单列表
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: integer
 *                   description: 状态码
 *                   example: 200
 *                 data:
 *                   type: array
 *                   description: 订单列表数据
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: integer
 *                         description: 订单 ID
 *                         example: 1
 *                       orderNo:
 *                         type: string
 *                         description: 订单号
 *                         example: "ORD20231201001"
 *                       userId:
 *                         type: integer
 *                         description: 用户 ID
 *                         example: 1
 *                       totalAmount:
 *                         type: number
 *                         description: 订单总金额
 *                         example: 299.99
 *                       status:
 *                         type: string
 *                         description: 订单状态
 *                         example: "pending"
 *                       createTime:
 *                         type: string
 *                         description: 创建时间
 *                         example: "2023-12-01 10:30:00"
 *       500:
 *         description: 暂无数据
 */
router.get("/order/all", (req, res) => {
    const sql = "select * from allorder"
    SQLConnect(sql, null, result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result
            })
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            })
        }
    })
})

/**
 * @swagger
 * /api/order/type:
 *   get:
 *     summary: 按类型获取订单
 *     description: 根据订单状态获取不同类型的订单
 *     tags: [订单模块]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: type
 *         required: true
 *         schema:
 *           type: integer
 *           enum: [1, 2, 3]
 *         description: 订单类型（1:待支付, 2:待收货, 3:待评价）
 *     responses:
 *       200:
 *         description: 成功获取订单列表
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 status:
 *                   type: integer
 *                   description: 状态码
 *                   example: 200
 *                 data:
 *                   type: array
 *                   description: 订单列表数据
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: integer
 *                         description: 订单 ID
 *                         example: 1
 *                       orderNo:
 *                         type: string
 *                         description: 订单号
 *                         example: "ORD20231201001"
 *                       userId:
 *                         type: integer
 *                         description: 用户 ID
 *                         example: 1
 *                       totalAmount:
 *                         type: number
 *                         description: 订单总金额
 *                         example: 299.99
 *                       status:
 *                         type: integer
 *                         description: 订单状态
 *                         example: 1
 *                       createTime:
 *                         type: string
 *                         description: 创建时间
 *                         example: "2023-12-01 10:30:00"
 *       500:
 *         description: 暂无数据
 */

router.get("/order/type", (req, res) => {
    var type = url.parse(req.url, true).query.type;
    const sql = "select * from allorder where status=?"
    SQLConnect(sql, [type], result => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result
            })
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            })
        }
    })
})


module.exports = router;
// nodemon